package com.hongkang.springtrains.repository.jpa;

import com.hongkang.springtrains.repository.UserRepository;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@PropertySource("/database.properties")
@EnableJpaRepositories(basePackageClasses = UserRepository.class)
public class JPAConfig {

    @Autowired
    private Environment environment;


    @Bean(destroyMethod = "close")
    public DataSource createDataSource(){
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUsername(environment.getProperty("dataSource.username"));
        basicDataSource.setPassword(environment.getProperty("dataSource.password"));
        basicDataSource.setUrl(environment.getProperty("dataSource.url"));
        basicDataSource.setDriverClassName(environment.getProperty("dataSource.driverClassName"));
        basicDataSource.setInitialSize(1);
        return basicDataSource;
    }


    @Bean
    public JpaVendorAdapter jpaVendorAdapter(DataSource dataSource){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.POSTGRESQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(false);
        adapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
        return adapter;

    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,JpaVendorAdapter adapter){
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(adapter);
        entityManagerFactoryBean.setPersistenceUnitName("SpringTrains");
        entityManagerFactoryBean.setPackagesToScan("com.hongkang.springtrains.model");
        return entityManagerFactoryBean;
    }



    //异常转换器,去掉注释无法导入环境变量
   /*@Bean
    public PersistenceAnnotationBeanPostProcessor postProcessor(){
        return  new PersistenceAnnotationBeanPostProcessor();
    }*/


    @Configuration
    @EnableTransactionManagement
    public static class TransactionConfig{

        @Autowired
        private EntityManagerFactory entityManagerFactory;

        @Bean
        public PlatformTransactionManager transactionManager(){
            JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory);
            return transactionManager;
        }




    }












}
